﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Data;
using System.Threading;
using System.Data.Objects;

namespace Scheduler
{
    class Program
    {
        static void Main(string[] args)
        {
            LunchpadEntities entities = new LunchpadEntities();
            var query = from user in entities.ATTENDINGs

                        where
                            user.IS_ATTENDING == 1

                        select user;

            string attendees = "";
            int i = 0;
            foreach (var result in query)
            {
                attendees = attendees + result.NAME_FIRST + " " + result.NAME_LAST + System.Environment.NewLine;
                i++;
            }
            // call the stored procedure to get the return value
            System.Nullable<int> iReturnValue = entities.findPerfectSpot(1).SingleOrDefault(); //resets the vote for the group
            if (iReturnValue.HasValue)
            {
                Console.WriteLine(iReturnValue);
                // return was successful!
                var rest = (from o in entities.RESTAURANTs where o.RESTAURANT_ID == iReturnValue select o).First();
             //   Console.WriteLine("You chose " + rest.RESTAURANT_NAME);

                

                foreach (var result in query)
                {
                    if (result.SEND_EMAIL == 1) {
                        MailMessage mm = new MailMessage();
                        mm.From = new MailAddress("admin@lunchpad.me");
                        mm.To.Add(new MailAddress(result.EMAIL));
                        string subject = "Grab a table for " + i + " at " + rest.RESTAURANT_NAME;
                        mm.Subject = subject;
                        string body = "LunchPad just added 15 minutes to your life.  You can thank us later.\n\nHave fun with your workmates:\n" + attendees;

                        mm.Body = body;
                        SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
                        sc.Credentials = new NetworkCredential("parivedalunchpad@gmail.com", "Pariveda1");
                        sc.EnableSsl = true;
                        sc.Send(mm);

                        Console.WriteLine("Email " + result.EMAIL);
                        Console.WriteLine(subject);
                        Console.WriteLine(body);
                    }
                    if (result.SEND_SMS == 1)
                    {
                        MailMessage mm = new MailMessage();
                        mm.From = new MailAddress("admin@lunchpad.me");
                        mm.To.Add(new MailAddress(result.SMS));
                        string subject = "Grab a table for " + i + " at " + rest.RESTAURANT_NAME;
                        mm.Subject = subject;
                        string body = "LunchPad just added 15 minutes to your life.  You can thank us later.\n\nHave fun with your workmates:\n" + attendees;

                        mm.Body = body;
                        SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
                        sc.Credentials = new NetworkCredential("parivedalunchpad@gmail.com", "Pariveda1");
                        sc.EnableSsl = true;
                        sc.Send(mm);

                        Console.WriteLine("SMS " + result.SMS);
                        Console.WriteLine(subject);
                        Console.WriteLine(body);
                    }
                }


             
            }
            
   
           

        }
    }
}
